package com.sitech.ibnms.util.charts.fusion.line;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sitech.ibnms.util.charts.domain.HIS_DATA_DOMAIN;
import com.sitech.ibnms.util.charts.fusion.FusionChartCommon;

/**
 * @author Mirror_Jean
 * 
 * @Date :Jan 5, 2011
 * @Time :10:38:03 AM
 * @Vesion :PluginLineChart
 */
public class PluginLineChart {

	public String getLineChart(FusionLineChartParameter parameter,
			List<HIS_DATA_DOMAIN> hisData) {
		
		return  FusionChartCommon.getScript(parameter).replaceAll("######", 
				this.setLineChartParameter(parameter, hisData).toString());
	}

	/**
	 * 
	 * @param parameter
	 * @param hisData
	 * @return
	 */
	public String setLineChartParameter(FusionLineChartParameter parameter,
			List<HIS_DATA_DOMAIN> hisData) {
		Document document = DocumentHelper.createDocument();
		try {
			Element chartHead = document.addElement("chart");
			String headAttribute[] = FusionLineChartParamName.paramName;
			String headAttributeValue[] = FusionLineChartParameter.getParamVaule(parameter,headAttribute);
			int idx = 0;
			for (String name : headAttribute) {
				chartHead.addAttribute(name, headAttributeValue[idx]);
				idx++;
			}
			setChildParam(chartHead, "set", hisData);
		} catch (Exception ex) {
			System.out.println("add root chart has exception ,method : setLineChartParameter();");
			ex.printStackTrace();
		}
		return formatXMLData(document);
	}

	private void setChildParam(Element parent, String childName,
			List<HIS_DATA_DOMAIN> childDataList) {
		try {
			int i = 0;
			if (childDataList != null)
				for (HIS_DATA_DOMAIN childData : childDataList) {
					Element chartNode = parent.addElement(childName);
					String cll_time = childData.getCLL_TIME().split(" ")[1];
					String value = childData.getKPI_VALUE()!=null?childData.getKPI_VALUE().toString():"0";
					chartNode.addAttribute("label", cll_time);
					chartNode.addAttribute("value", value);
					chartNode.addAttribute("showValue", "0");
					chartNode.addAttribute("showLabel", "1");
					chartNode.addAttribute("toolText", "(ʱ��:"+cll_time+",����:"+value+")");
				}
		} catch (Exception ex) {
			System.out.println("add child chart node has exception ,method : setChildParam();");
			ex.printStackTrace();
		}
	}

	private void setChartStyle(Element parent, String childName) {

	}

	private String formatXMLData(Document document) {
		String xml[] = document.asXML().split("\n");
		System.out.println("xml size : " + xml.length);
		return xml[1].replace("\"", "\'");
	}
}
